package com.syh.jn.scm.domain.upstream.entity.system;

import cn.redscarf.commons.util.bean.ExtBeanUtils;
import cn.redscarf.commons.util.other.ExtAssert;
import cn.redscarf.commons.spring.SpringContextUtils;
import cn.redscarf.commons.util.validator.ValidationUtils;
import cn.redscarf.commons.wrapper.Restrictions;
import cn.redscarf.commons.wrapper.Wrapper;
import com.syh.jn.scm.domain.support.SuperBusinessAggregate;
import com.syh.jn.scm.domain.upstream.repository.system.UpAccountRepository;
import com.syh.jn.scm.domain.upstream.repository.system.UpMenuRepository;
import com.syh.jn.scm.domain.upstream.repository.system.UpRoleRepository;
import com.syh.jn.scm.dto.operation.system.permission.PermissionVo;
import com.syh.jn.scm.dto.upstream.system.upRole.UpRoleMto;
import com.syh.jn.scm.dto.upstream.system.upRole.UpRoleSto;
import com.syh.jn.scm.exception.BusinessException;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;

import java.util.Collections;
import java.util.List;

@Data
@NoArgsConstructor
@EqualsAndHashCode(callSuper = true)
public class UpRole extends SuperBusinessAggregate<Long> {


  public UpRole(UpRoleSto sto) {
    ExtAssert.notNull(sto, "对象为空");
    ValidationUtils.validate(sto);
    this.terminalType = "UPSTREAM";
    this.createTimeStamp = System.currentTimeMillis();
    ExtBeanUtils.copyProperties(sto, this, false);
  }

  public void modify(UpRoleMto mto) {
    ExtAssert.notNull(mto, "对象为空");
    ValidationUtils.validate(mto);
    this.lastUpdateTimeStamp = System.currentTimeMillis();
    ExtBeanUtils.copyProperties(mto, this, false);
  }

  public void updateMenus(List<Long> menuIds) {
    if (menuIds.isEmpty()) {
      this.menuList = Collections.emptyList();
      return;
    }
    UpMenuRepository upstreamMenuRepository = (UpMenuRepository) SpringContextUtils.getBean(UpMenuRepository.class);
    List<UpMenu> menuList = upstreamMenuRepository.getList(Wrapper.build().append(Restrictions.in("id", menuIds.toArray())));
    this.menuList = menuList;
  }

  /**
   * 数据删除
   */
  public void destroy() {
    UpRoleRepository supplierRoleRepository = (UpRoleRepository) SpringContextUtils.getBean(UpRoleRepository.class);
    List<PermissionVo> permission = supplierRoleRepository.getPermissionByRoleId(id);
    if (permission != null && permission.size() > 0) {
      throw new BusinessException("该角色存在菜单,不能删除");
    }
    UpAccountRepository accountRepository = (UpAccountRepository) SpringContextUtils.getBean(UpAccountRepository.class);
    Wrapper wrapper = Wrapper.build();
    wrapper.append(
            Restrictions.and(Restrictions.eq("roleId", id))
    );
    List accounts = accountRepository.findListPage(wrapper, 0, 1000);
    if (accounts != null && accounts.size() > 0) {
      throw new BusinessException("角色不能删除,已有账号正在使用");
    }
  }

  /**
   * 角色名称
   */
  private String roleName;
  private String terminalType;
  private String description;
  private Integer orderLevel;
  private List<UpMenu> menuList;
  private Long businessEntityId;
}
